package log;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;

public class CSVWorker {
	
	String filePath;
	String fileName;
	// по замовчуванню кидаєм в...
	public CSVWorker(){
		setFilePath("ServerData/");
		setFileName("logs.csv");
		try {
	         if (!new File("ServerData").exists())
	            new File("ServerData").mkdir();
	         if (!new File("ServerData/logs.csv").exists())
	            new File("ServerData/logs.csv").createNewFile();
	      } catch (IOException e) {
	         System.err.println(e);
	      }
	}
	
	public CSVWorker(String filePath, String fileName){
		setFilePath(filePath);
		setFileName(fileName);
		try {
	         if (!new File(filePath).exists())
	            new File(filePath).mkdir();
	         if (!new File(getFilePath().concat(getFileName())).exists())
	            new File(getFilePath().concat(getFileName())).createNewFile();
	      } catch (IOException e) {
	         System.err.println(e);
	      }
	}
	
	public void log(int id, String file, boolean result){
		String dateTime=new java.text.SimpleDateFormat("dd.mm.yy HH:mm").format(java.util.Calendar.getInstance ().getTime());
		PrintWriter writer = null;
		try
			{
			writer = new PrintWriter(new FileOutputStream(getFilePath().concat(getFileName()), true));
			}
			catch(FileNotFoundException e)
			{
				System.err.println(e);
			}
		String log = new String();
		log = String.valueOf(id);          
		log = log.concat(","+file);		   
		log = log.concat(","+result);
		log = log.concat(","+dateTime);
		writer.append(log);
		writer.close();
}

	public String getFilePath() {
		return filePath;
	}

	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
	
}